package com.gimi.cloud.jimi.manage.service.app.impl;

import com.gimi.cloud.jimi.common.enums.ResponseCodeEnum;
import com.gimi.cloud.jimi.common.exception.MsgException;
import com.gimi.cloud.jimi.common.utils.DateUtil;
import com.gimi.cloud.jimi.common.utils.Page;
import com.gimi.cloud.jimi.commonService.common.JmOperationLogService;
import com.gimi.cloud.jimi.dao.dao.homePage.ColumnDao;
import com.gimi.cloud.jimi.dao.dao.homePage.ColumnProdDao;
import com.gimi.cloud.jimi.dao.dao.product.ProductDao;
import com.gimi.cloud.jimi.dto.app.ColumnProdDTO;
import com.gimi.cloud.jimi.entity.app.ColumnEntity;
import com.gimi.cloud.jimi.entity.app.ColumnProdEntity;
import com.gimi.cloud.jimi.entity.product.ProductEntity;
import com.gimi.cloud.jimi.manage.service.app.ColumnProdService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * @auther loongs
 * @date 2018/1/16  16:48
 */
@Service
public class ColumnProdServiceImpl implements ColumnProdService {
    private JmOperationLogService jmOperationLogService;
    private ColumnProdDao columnProdDao;
    private ColumnDao columnDao;
    private ProductDao productDao;

    @Autowired
    public ColumnProdServiceImpl(JmOperationLogService jmOperationLogService, ColumnProdDao columnProdDao, ColumnDao columnDao, ProductDao productDao) {
        this.jmOperationLogService = jmOperationLogService;
        this.columnProdDao = columnProdDao;
        this.columnDao = columnDao;
        this.productDao = productDao;
    }


    @Override
    public Page<ColumnProdDTO> getColumnProdList(ColumnProdDTO columnProdDTO) {
        PageHelper.startPage(columnProdDTO.getCurrentPage(), columnProdDTO.getPageSize());
        List<ColumnProdDTO> result = columnProdDao.queryPageList(columnProdDTO);
        Page<ColumnProdDTO> page = new Page(new PageInfo<>(result));
        return page;
    }

    @Override
    public boolean addColumnProd(ColumnProdDTO columnProdDTO) {
        //校验ColumnEntity是否存在
        ColumnEntity columnEntityTemp = columnDao.selectByPrimaryKey(columnProdDTO.getColumnId());
        if (columnEntityTemp == null) {
            throw new MsgException(ResponseCodeEnum.ADD_COLUMNPROD_FAIL_COLUMN_IS_NOT_FOUND);
        }
        //校验商品是否存在
        ProductEntity productEntity = productDao.selectByPrimaryKey(columnProdDTO.getProdId());
        if (productEntity == null) {
            throw new MsgException(ResponseCodeEnum.ADD_COLUMNPROD_FAIL_PRODUCT_IS_EXIT);
        }
        //校验 商品在该栏目下是否存在
        ColumnProdEntity columnProdEntityTemp = columnProdDao.selectColumnProdByColumnIdAndProdId(columnProdDTO);
        if (columnProdEntityTemp != null) {
            throw new MsgException(ResponseCodeEnum.ADD_COLUMNPROD_FAIL_COLUMNPROD_IS_EXIT);
        }

        ColumnProdEntity columnProdEntity = new ColumnProdEntity();
        BeanUtils.copyProperties(columnProdDTO, columnProdEntity);
        columnProdEntity.setCreateTime(DateUtil.getNow());
        Integer flag = columnProdDao.insertSelective(columnProdEntity);

        if (flag > 0) {
            //添加操作日志
            jmOperationLogService.insertAddLog(columnProdDTO, columnProdDTO.getColumnId() + "", "addColumnProd",
                    "jimi_column_prod", "添加栏目下的商品");
            return true;
        }
        return false;
    }

    @Override
    public void addBatchColumnProd(ColumnProdDTO columnProdDTO) {
        List<Map<String, Object>> addBatchParam = columnProdDTO.getAddBatchParam();
        ColumnProdEntity columnProdEntity = null;
        StringBuffer sb =new StringBuffer();
        for (Map<String, Object> map : addBatchParam) {
            columnProdEntity = new ColumnProdEntity();
            columnProdEntity.setColumnId(columnProdDTO.getColumnId());
            columnProdEntity.setSort(map.get("sort").toString());
            columnProdEntity.setProdId(map.get("prodId").toString());
            columnProdEntity.setCreateTime(DateUtil.getNow());
            columnProdDao.insertUseGeneratedKeys(columnProdEntity);
            sb.append(columnProdEntity.getId()+",");
        }
        sb.deleteCharAt(sb.length() - 1);
        //添加操作日志
        jmOperationLogService.insertAddLog(columnProdDTO, sb+ "", "addBatchColumnProd",
                "jimi_column_prod", "批量添加栏目下的商品");
    }

    @Override
    public List<ProductEntity> goToaddColumnProd(ColumnProdDTO columnProdDTO) {
        return productDao.goToaddColumnProd(columnProdDTO);
    }

    @Override
    public ColumnProdEntity goToEditColumnProd(ColumnProdDTO columnProdDTO) {
        return columnProdDao.selectByPrimaryKey(columnProdDTO.getId());
    }

    @Override
    public boolean editColumnProd(ColumnProdDTO columnProdDTO) {
        ColumnProdEntity columnProdEntity = columnProdDao.selectByPrimaryKey(columnProdDTO);
        columnProdEntity.setSort(columnProdDTO.getSort());
        columnProdEntity.setModifyTime(DateUtil.getNow());
        Integer flag = columnProdDao.updateByPrimaryKey(columnProdEntity);
        if (flag > 0) {
            //添加操作日志
            jmOperationLogService.insertUpdateLog(columnProdDTO, "editColumnProd",
                    "jimi_column_prod", columnProdDTO.getColumnId() + "", "修改栏目下的商品");
            return true;
        }
        return false;
    }

    @Override
    public boolean delColumnProd(ColumnProdDTO columnProdDTO) throws MsgException {
        ColumnProdEntity columnProdEntity = new ColumnProdEntity();
        BeanUtils.copyProperties(columnProdDTO, columnProdEntity);
        columnProdDao.deleteByPrimaryKey(columnProdEntity);

        ColumnProdEntity columnProdEntityTemp = columnProdDao.selectByPrimaryKey(columnProdDTO);
        if (columnProdEntityTemp == null) {
            jmOperationLogService.insertDeleteLog(columnProdDTO.getAddUid(), "delColumnProd", "jimi_column_prod",
                    columnProdDTO.getId() + "", "删除栏目下的商品");
            return true;
        }
        return false;
    }


}
